<?php
namespace app\admin\controller;

use think\facade\Config;
use think\facade\Session;
use app\admin\model\AdminLog;
use app\admin\model\AuthRule as RuleModel;
use app\common\controller\Base as CommonBase;

class Base extends CommonBase
{
    public function initialize()
    {
        parent::initialize();
        $this->current_member = login_member();
        if(empty($this->current_member)){
          if($this->request->isAjax()){
            $this->error('请登录', 'login/login', '', 0);
          }else{
            $this->redirect('login/login');
          }
        }
        $this->assign('current_member', $this->current_member);

        $AuthModel = new RuleModel;
        $this->menu_tree = $AuthModel->memberTreeMenu();
        $this->assign('auth', $AuthModel);
        $this->assign('menu_tree', $this->menu_tree);

        // 判断用户是否拥有当前控制器访问权限
        $current_page = $AuthModel->checkCurrentPage();
        if(is_null($current_page)){
          if($this->request->isAjax()){
            $this->error('请登录', 'index/index', '', 0);
          }else{
            $this->redirect('index/index');
          }
        }

        if(empty($current_page)){
          $current_ancestors = [];
        }else{
          $current_ancestors = $current_page['with_ancestors'];
        }
        $this->assign("current_page", $current_page);
        $this->assign("current_ancestors", $current_ancestors);

        if($this->request->isGet() && !$this->request->isAjax()){
            $this->readFlushMessage();
        }

        $LogModel = new AdminLog();
        $res = $LogModel->add();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $param = $this->request->param();
        $default_param = $this->index_default_param();
        $res = $this->Model->minePaginate(array_merge($param, $default_param));
        $this->assign("param", $param);
        $this->assign("res", $res);
        return $this->fetch();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id = 0)
    {
        $this->redirect('index');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id = 0)
    {
        if($this->request->isDelete()){
            // GroupModel::destroy($id);
            $res = $this->Model->deleteItem($id);
            if($res['status']){
                Session::flash('success', $res['msg']);
                $this->success($res['msg'], url('index'));
            }else{
                Session::flash('danger', $res['msg']);
                $this->error($res['msg'], url('index'));
            }

        }else{
            $this->error('您请求的方法不存在', url('index'));
        }
    }

    public function resort(){
        // 因为items是数组，所以下面两行不能合并写成 $this->request->param('items');
        $param = $this->request->param();
        $res = $this->Model->resortItems($param['items']);
        if($res['status']){
            Session::flash('success', $res['msg']);
        }else{
            Session::flash('danger', $res['msg']);
        }
        $this->redirect('index');
    }

    /**
     * 验证字段值是否唯一
     * @Author   Laifuzi
     * @DateTime 2018-06-06
     * @return   [type]     [description]
     */
    public function ajax_check_unique(){
        if(!$this->request->isPost()){
            return false;
        }

        $field_name = $this->request->post('field');
        if(empty($field_name)){
            return false;
        }
        $field_value = $this->request->post($field_name);

        $id = $this->request->post('id');

        $valid = $this->Model->isFieldValueUnique($field_name, $field_value, $id);
        return $valid;
    }
}
